package cn.com.generaldata.os.service.impl.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.nutz.dao.Dao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.com.generaldata.jsme_indicator_system.entity.AuthorFunc;
import cn.com.generaldata.jsme_indicator_system.entity.Authority;
import cn.com.generaldata.jsme_indicator_system.entity.BiIndicatorNz;
import cn.com.generaldata.jsme_indicator_system.entity.BiShowNz;
import cn.com.generaldata.jsme_indicator_system.entity.Dustry;
import cn.com.generaldata.jsme_indicator_system.entity.Func;
import cn.com.generaldata.jsme_indicator_system.entity.Indicator;
import cn.com.generaldata.jsme_indicator_system.entity.ReportInputTitle;
import cn.com.generaldata.jsme_indicator_system.entity.ReportTypeIndicator;
import cn.com.generaldata.jsme_indicator_system.entity.ReportTypeLimit;
import cn.com.generaldata.jsme_indicator_system.entity.ReportTypeNz;
import cn.com.generaldata.jsme_indicator_system.entity.ReportTypeUnit;
import cn.com.generaldata.jsme_indicator_system.entity.ReportTypeUnitType;
import cn.com.generaldata.jsme_indicator_system.entity.ReportVersion;
import cn.com.generaldata.jsme_indicator_system.entity.StatSystem;
import cn.com.generaldata.jsme_indicator_system.entity.UnitNz;
import cn.com.generaldata.jsme_indicator_system.entity.UnitRelationNz;
import cn.com.generaldata.os.constant.FilterConstant;
import cn.com.generaldata.os.dao.intf.BaseDAOInterface;
import cn.com.generaldata.os.paging.Filter;
import cn.com.generaldata.os.paging.FilterRule;
import cn.com.generaldata.os.pojo.Area;
import cn.com.generaldata.os.pojo.AreaPerform;
import cn.com.generaldata.os.pojo.DustryReport;
import cn.com.generaldata.os.pojo.EnterpriseIndustry;
import cn.com.generaldata.os.pojo.Industry;
import cn.com.generaldata.os.pojo.LArea;
import cn.com.generaldata.os.pojo.Report;
import cn.com.generaldata.os.pojo.ReportDetail;
import cn.com.generaldata.os.pojo.ReportHistory;
import cn.com.generaldata.os.pojo.ReportMarking;
import cn.com.generaldata.os.pojo.ReportStaticContent;
import cn.com.generaldata.os.pojo.ReportUnit;
import cn.com.generaldata.os.pojo.Role;
import cn.com.generaldata.os.pojo.Unit;
import cn.com.generaldata.os.pojo.User;
import cn.com.generaldata.os.pojo.UserIndustry;

import com.google.common.collect.Lists;

@Component
public class ReportEIServiceForWeb {
	private static Logger log = LoggerFactory.getLogger(ReportEIServiceForWeb.class);

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<Report> reportDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<ReportDetail> reportDetailDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<DustryReport> dustryReportDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<ReportUnit> reportUnitDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<ReportMarking> reportMarkingDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<ReportHistory> reportHistoryDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<User> userDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<Industry> industryDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<UserIndustry> userIndustryDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<AreaPerform> areaPerformDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<ReportStaticContent> reportStaticDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<EnterpriseIndustry> enterpriseIndustryDAO;
	private final ExecutorService executorService = Executors.newSingleThreadExecutor();
	private final ExecutorService importService = Executors.newSingleThreadExecutor();

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.exportpojo.Unit> unitDAOExport;

	public void setUnitDAOExport(BaseDAOInterface<cn.com.generaldata.os.exportpojo.Unit> unitDAOExport) {
		this.unitDAOExport = unitDAOExport;
	}

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.newpojo.ReportType> reportTypeDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.newpojo.ReportVersion> reportVersionDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.newpojo.SystemCodedic> systemDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.pojo.Unit> unitDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.SystemCodedic> systemDAOImport;
	private boolean exportRunning = false;

	private boolean importRunning;

	@Autowired
	@Qualifier("indicatorSystemDao")
	private Dao indicatorSystemDao;

	@Autowired
	private Dao daoExport;

	@Autowired
	private Dao daoImport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.exportpojo.User> userDAOExport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.Area> areaDAOImport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.pojo.Area> areaDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.pojo.LArea> lareaDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.LArea> lareaDAOImport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.exportpojo.AreaPerform> areaPerformDAOExport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.Unit> unitDAOImport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.exportpojo.UserIndustry> userIndustryDAOExport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.EnterpriseIndustry> enterpriseIndustryDAOImport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.DustryReport> dustryReportDAOImport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.exportpojo.ReportDetail> reportDetailDAOExport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.exportpojo.ReportMarking> reportMarkingDAOExport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.exportpojo.ReportHistory> reportHistoryDAOExport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.Industry> industryDAOImport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.exportpojo.ReportStaticContent> reportStaticDAOExport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.ReportStaticContent> reportStaticDAOImport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.Report> reportDAOImport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.ReportDetail> reportDetailDAOImport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.ReportMarking> reportMarkingDAOImport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.ReportHistory> reportHistoryDAOImport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.UserIndustry> userIndustryDAOImport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.User> userDAOImport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.ReportUnit> reportUnitImportDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.importpojo.AreaPerform> areaPerformDAOImport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.exportpojo.ReportType> reportTypeDAOExport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.exportpojo.ReportUnit> reportUnitExportDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.exportpojo.ReportVersion> reportVersionDAOExport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.exportpojo.SystemCodedic> systemDAOExport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.exportpojo.ReportInputTitle> reportTitleDAOExport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.exportpojo.ReportTypeIndicator> reportTypeIndicatorDAOExport;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<cn.com.generaldata.os.exportpojo.Report> reportDAOExport;

	// //导出所有报表模板

	public Boolean exportReportType() {
		if (!exportRunning) {
			executorService.execute(new ExportTypeTask());
		}
		return true;
	}

	class ExportTypeTask implements Runnable {

		@Override
		public void run() {
			importRunning = true;
			// SystemCodedic同步
			try {
				List<cn.com.generaldata.os.importpojo.SystemCodedic> systemList = systemDAOImport.find(
						cn.com.generaldata.os.importpojo.SystemCodedic.class, null, null);
				List<cn.com.generaldata.os.newpojo.SystemCodedic> sysCodes = systemDAO.find(
						cn.com.generaldata.os.newpojo.SystemCodedic.class, null, null);
				if (systemList != null && systemList.size() > 0) {
					systemDAO.delete(sysCodes);
					for (cn.com.generaldata.os.importpojo.SystemCodedic sc : systemList) {

						cn.com.generaldata.os.newpojo.SystemCodedic sysCode = new cn.com.generaldata.os.newpojo.SystemCodedic();

						BeanUtils.copyProperties(sc, sysCode);
						systemDAO.save(sysCode);
					}
				}
				// ReportInputTitle同步
				List<ReportInputTitle> queryList = daoImport.query(ReportInputTitle.class, null);
				if (queryList != null && queryList.size() > 0) {
					indicatorSystemDao.clear(ReportInputTitle.class);
					indicatorSystemDao.insert(queryList);
				}

				// ReportTypeLimit同步
				List<ReportTypeLimit> limitList = daoImport.query(ReportTypeLimit.class, null);
				if (limitList != null && limitList.size() > 0) {

					indicatorSystemDao.clear(ReportTypeLimit.class);
					indicatorSystemDao.insert(limitList);
				}

				// UnitRelationNz同步
				List<UnitRelationNz> unitRelation = daoImport.query(UnitRelationNz.class, null);
				if (unitRelation != null && unitRelation.size() > 0) {

					indicatorSystemDao.clear(UnitRelationNz.class);
					indicatorSystemDao.insert(unitRelation);
				}

				// tbl_authority同步
				List<Authority> authorityList = daoImport.query(Authority.class, null);
				if (authorityList != null && authorityList.size() > 0) {

					indicatorSystemDao.clear(Authority.class);
					indicatorSystemDao.insert(authorityList);
				}

				// tbl_author_func同步
				List<AuthorFunc> authorFuncList = daoImport.query(AuthorFunc.class, null);
				if (authorFuncList != null && authorFuncList.size() > 0) {

					indicatorSystemDao.clear(AuthorFunc.class);
					indicatorSystemDao.insert(authorFuncList);
				}

				// tbl_func同步
				List<Func> funcList = daoImport.query(Func.class, null);
				if (funcList != null && funcList.size() > 0) {

					indicatorSystemDao.clear(Func.class);
					indicatorSystemDao.insert(funcList);
				}
				// StatSystem同步
				List<StatSystem> statList = daoImport.query(StatSystem.class, null);
				if (statList != null && statList.size() > 0) {

					indicatorSystemDao.clear(StatSystem.class);
					indicatorSystemDao.insert(statList);
				}

				// DustryReport同步
				List<cn.com.generaldata.os.importpojo.DustryReport> dustryReportList = dustryReportDAOImport.find(
						cn.com.generaldata.os.importpojo.DustryReport.class, null, null);
				List<DustryReport> find = dustryReportDAO.find(cn.com.generaldata.os.pojo.DustryReport.class, null,
						null);
				if (dustryReportList != null && dustryReportList.size() > 0) {

					dustryReportDAO.delete(find);
					for (cn.com.generaldata.os.importpojo.DustryReport rt : dustryReportList) {
						cn.com.generaldata.os.pojo.DustryReport rtExport = new cn.com.generaldata.os.pojo.DustryReport();
						BeanUtils.copyProperties(rt, rtExport);
						if (rt.getIndustry1() != null) {
							Industry in = new Industry();
							in.setId(rt.getIndustry1());
							rtExport.setIndustry(in);
						}
						dustryReportDAO.save(rtExport);
					}
				}
				// ReportType同步
				List<ReportTypeNz> reportTypeList = daoImport.query(ReportTypeNz.class, null, null);
				if (reportTypeList != null && reportTypeList.size() > 0) {
					indicatorSystemDao.clear(ReportTypeNz.class);
					indicatorSystemDao.insert(reportTypeList);
				}

				//BiIndicator同步
				List<BiIndicatorNz> biList = daoImport.query(BiIndicatorNz.class, null);
				if (biList != null && biList.size() > 0) {
					indicatorSystemDao.clear(BiIndicatorNz.class);
					indicatorSystemDao.insert(biList);
				}

				//BiShow同步
				List<BiShowNz> biShowList = daoImport.query(BiShowNz.class, null);
				if (biShowList != null && biShowList.size() > 0) {
					indicatorSystemDao.clear(BiShowNz.class);
					indicatorSystemDao.insert(biShowList);
				}

				// ReportVersion同步
				List<ReportVersion> reportVersionList = daoImport.query(ReportVersion.class, null, null);
				if (reportVersionList != null && reportVersionList.size() > 0) {
					indicatorSystemDao.clear(ReportVersion.class);
					indicatorSystemDao.insert(reportVersionList);
				}

				// Indicator同步
				List<Indicator> indicator = daoImport.query(Indicator.class, null);
				if (indicator != null && indicator.size() > 0) {

					indicatorSystemDao.clear(Indicator.class);
					indicatorSystemDao.insert(indicator);
				}
				// ReportTypeIndicator同步
				List<ReportTypeIndicator> indicatorList = daoImport.query(ReportTypeIndicator.class, null);
				if (indicatorList != null && indicatorList.size() > 0) {

					indicatorSystemDao.clear(ReportTypeIndicator.class);
					indicatorSystemDao.insert(indicatorList);
				}

				// ReportTypeUnitType同步
				List<ReportTypeUnitType> tutList = daoImport.query(ReportTypeUnitType.class, null);
				if (tutList != null && tutList.size() > 0) {

					indicatorSystemDao.clear(ReportTypeUnitType.class);
					indicatorSystemDao.insert(tutList);
				}

				// AreaPerform同步
				List<cn.com.generaldata.os.importpojo.AreaPerform> apList = areaPerformDAOImport.find(
						cn.com.generaldata.os.importpojo.AreaPerform.class, null, null);
				List<AreaPerform> find2 = areaPerformDAO.find(cn.com.generaldata.os.pojo.AreaPerform.class, null, null);
				if (apList != null && apList.size() > 0) {
					areaPerformDAO.delete(find2);
					for (cn.com.generaldata.os.importpojo.AreaPerform ap : apList) {
						cn.com.generaldata.os.pojo.AreaPerform areaPerformE = new cn.com.generaldata.os.pojo.AreaPerform();
						BeanUtils.copyProperties(ap, areaPerformE);
						areaPerformDAO.save(areaPerformE);

					}
				}
				// tbl_dustry同步
				List<Dustry> dusList = daoImport.query(Dustry.class, null);
				if (dusList != null && dusList.size() > 0) {
					indicatorSystemDao.clear(Dustry.class);
					indicatorSystemDao.insert(dusList);
				}
				// Report_static_content同步
				List<cn.com.generaldata.os.importpojo.ReportStaticContent> rsList = reportStaticDAOImport.find(
						cn.com.generaldata.os.importpojo.ReportStaticContent.class, null, null);
				if (rsList != null && rsList.size() > 0) {
					List<ReportStaticContent> find3 = reportStaticDAO.find(
							cn.com.generaldata.os.pojo.ReportStaticContent.class, null, null);
					reportStaticDAO.delete(find3);
					for (cn.com.generaldata.os.importpojo.ReportStaticContent rs : rsList) {
						cn.com.generaldata.os.pojo.ReportStaticContent rsE = new cn.com.generaldata.os.pojo.ReportStaticContent();
						BeanUtils.copyProperties(rs, rsE);
						reportStaticDAO.save(rsE);

					}
				}
				// ReportTypeUnit同步
				List<ReportTypeUnit> rtuList = daoImport.query(ReportTypeUnit.class, null);
				if (rsList != null && rsList.size() > 0) {
					indicatorSystemDao.clear(ReportTypeUnit.class);
					indicatorSystemDao.insert(rtuList);

				}
			} catch (BeansException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				importRunning = false;
			}
			importRunning = false;

			System.out.print("type clear!");
		}
	}

	// 首先查看ReportStaticID是已经在导出库中存在，没有则添加，否则跳过
	public boolean isReportStaticIdExist(Integer rsId) {
		cn.com.generaldata.os.exportpojo.ReportStaticContent rs = reportStaticDAOExport.get(
				cn.com.generaldata.os.exportpojo.ReportStaticContent.class, rsId);
		if (rs != null) {
			return true;
		}
		return false;
	}

	// 首先查看areaPerformID是已经在导出库中存在，没有则添加，否则跳过
	public boolean isAreaPerformIdExist(String apId) {
		cn.com.generaldata.os.pojo.AreaPerform ap = areaPerformDAO.get(cn.com.generaldata.os.pojo.AreaPerform.class,
				apId);
		if (ap != null) {
			return true;
		}
		return false;
	}

	public boolean isRSCIdExist(Integer apId) {
		cn.com.generaldata.os.pojo.ReportStaticContent ap = reportStaticDAO.get(
				cn.com.generaldata.os.pojo.ReportStaticContent.class, apId);
		if (ap != null) {
			return true;
		}
		return false;
	}

	class ExportTask implements Runnable {
		private final Set<String> rids;

		public ExportTask(Set<String> rids) {
			this.rids = rids;
		}

		@Override
		public void run() {
			exportRunning = true;
			// TODO Auto-generated method stub
			List<List<String>> parReports = Lists.partition(Lists.newArrayList(rids), 10);
			for (List<String> parReport : parReports) {

				try {
					exportReport(parReport);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					continue;
				}
			}
			exportRunning = false;

			System.out.print("clear!");
		}

		private void exportReport(List<String> parReport) {
			Filter reportFilter = new Filter();
			reportFilter.addRule(new FilterRule(FilterConstant.COLLECTION, "id", parReport.toArray()));
			//				reportFilter.addRule(new FilterRule(FilterConstant.OR,
			//						"reportReferenceId", ""));
			// 需导出的报表
			List<Report> reportList = reportDAO.find(Report.class, reportFilter, null);
			if (reportList != null && reportList.size() > 0) {

				for (Report report : reportList) {
					// 导出报表
					cn.com.generaldata.os.exportpojo.Report reportE = reportDAOExport.get(
							cn.com.generaldata.os.exportpojo.Report.class, report.getId());
					if (reportE != null) {
						reportDAOExport.delete(reportE);
					}
					reportE = new cn.com.generaldata.os.exportpojo.Report();
					BeanUtils.copyProperties(report, reportE);
					if (report.getArea() != null) {
						reportE.setArea1(report.getArea().getId());
					}
					if (report.getOperator() != null) {
						reportE.setOperator1(report.getOperator().getId());
					}
					if (report.getUser() != null) {
						reportE.setUser1(report.getUser().getId());
					}
					reportDAOExport.save(reportE);

					report.setReportFlag("1");
					reportDAO.update(report);
					//导出报表审批记录
					Filter unitFilter = new Filter();
					unitFilter.addRule(new FilterRule(FilterConstant.EQUAL, "reportId", report.getId()));
					List<ReportUnit> ruList = reportUnitDAO.find(ReportUnit.class, unitFilter, null);
					List<cn.com.generaldata.os.exportpojo.ReportUnit> find = reportUnitExportDAO.find(
							cn.com.generaldata.os.exportpojo.ReportUnit.class, unitFilter, null);
					if (ruList != null) {
						reportUnitExportDAO.delete(find);
						for (ReportUnit ru : ruList) {
							// 导出报表审批记录
							cn.com.generaldata.os.exportpojo.ReportUnit ruE = new cn.com.generaldata.os.exportpojo.ReportUnit();
							BeanUtils.copyProperties(ru, ruE);
							//								if (rd.getReport() != null) {
							//									rdE.setReport1(rd.getReport().getId());
							//								}
							reportUnitExportDAO.save(ruE);
						}
					}
					Filter detailFilter = new Filter();
					detailFilter.addRule(new FilterRule(FilterConstant.EQUAL, "report.id", report.getId()));
					List<ReportDetail> rdList = reportDetailDAO.find(ReportDetail.class, detailFilter, null);
					Filter detailExportFilter = new Filter();
					detailExportFilter.addRule(new FilterRule(FilterConstant.EQUAL, "report1", report.getId()));
					List<cn.com.generaldata.os.exportpojo.ReportDetail> find2 = reportDetailDAOExport.find(
							cn.com.generaldata.os.exportpojo.ReportDetail.class, detailExportFilter, null);

					if (rdList != null) {
						reportDetailDAOExport.delete(find2);
						for (ReportDetail rd : rdList) {

							// 导出报表详细
							cn.com.generaldata.os.exportpojo.ReportDetail rdE = new cn.com.generaldata.os.exportpojo.ReportDetail();
							BeanUtils.copyProperties(rd, rdE);
							if (rd.getReport() != null) {
								rdE.setReport1(rd.getReport().getId());
							}
							reportDetailDAOExport.saveOrUpdate(rdE);

						}
					}
					List<ReportHistory> rhList = reportHistoryDAO.find(ReportHistory.class, detailFilter, null);
					List<cn.com.generaldata.os.exportpojo.ReportHistory> find3 = reportHistoryDAOExport.find(
							cn.com.generaldata.os.exportpojo.ReportHistory.class, detailExportFilter, null);

					if (rhList != null) {
						reportHistoryDAOExport.delete(find3);
						for (ReportHistory rh : rhList) {

							// 导出报表历史
							cn.com.generaldata.os.exportpojo.ReportHistory rhE = new cn.com.generaldata.os.exportpojo.ReportHistory();
							BeanUtils.copyProperties(rh, rhE);
							if (rh.getOperator() != null) {
								rhE.setOperator1(rh.getOperator().getId());
							}
							if (rh.getReport() != null) {
								rhE.setReport1(rh.getReport().getId());
							}
							reportHistoryDAOExport.save(rhE);

						}
					}
					List<ReportMarking> rmList = reportMarkingDAO.find(ReportMarking.class, detailFilter, null);
					List<cn.com.generaldata.os.exportpojo.ReportMarking> find4 = reportMarkingDAOExport.find(
							cn.com.generaldata.os.exportpojo.ReportMarking.class, detailExportFilter, null);
					if (rmList != null) {
						reportMarkingDAOExport.delete(find4);
						for (ReportMarking rm : rmList) {

							// 导出报表标签
							cn.com.generaldata.os.exportpojo.ReportMarking rmE = new cn.com.generaldata.os.exportpojo.ReportMarking();
							BeanUtils.copyProperties(rm, rmE);
							if (rm.getOperator() != null) {
								rmE.setOperator1(rm.getOperator().getId());
							}
							if (rm.getReport() != null) {
								rmE.setReport1(rm.getReport().getId());
							}
							reportMarkingDAOExport.save(rmE);

						}
					}
				}
			}
		}

	}

	// 将业务库的终审通过或驳回报表导到导出库

	public Boolean exportEIReport(Set<String> rids) {
		if (!exportRunning) {
			executorService.execute(new ExportTask(rids));
		}
		return true;
	}

	// 查看单位ID是已经在导出库中存在，没有则添加，否则跳过
	public cn.com.generaldata.os.pojo.Unit isUnitIdExist(String unitId) {
		//		Filter unitFilter = new Filter();
		//		unitFilter.addRule(new FilterRule(FilterConstant.EQUAL, "id", unitId));
		//		List<cn.com.generaldata.os.pojo.Unit> unitList = unitDAO.find(cn.com.generaldata.os.pojo.Unit.class,
		//				unitFilter, null);
		Unit unit = unitDAO.get(Unit.class, unitId);
		if (unit != null) {
			return unit;
		}
		return null;
	}

	public cn.com.generaldata.os.pojo.Area isAreaIdExist(String areaId) {
		//		Filter unitFilter = new Filter();
		//		unitFilter.addRule(new FilterRule(FilterConstant.EQUAL, "id", areaId));
		//		List<cn.com.generaldata.os.pojo.Area> unitList = areaDAO.find(cn.com.generaldata.os.pojo.Area.class,
		//				unitFilter, null);
		Area area = areaDAO.get(Area.class, areaId);
		if (area != null) {
			return area;
		}
		return null;
	}

	public cn.com.generaldata.os.pojo.LArea isLAreaIdExist(String lareaId) {
		//		Filter unitFilter = new Filter();
		//		unitFilter.addRule(new FilterRule(FilterConstant.EQUAL, "id", lareaId));
		//		List<cn.com.generaldata.os.pojo.LArea> unitList = lareaDAO.find(cn.com.generaldata.os.pojo.LArea.class,
		//				unitFilter, null);
		//
		//		if (unitList != null && unitList.size() > 0) {
		//			return unitList.get(0);
		//		}
		LArea area = lareaDAO.get(LArea.class, lareaId);
		if (area != null) {
			return area;
		}
		return null;
	}

	// 首先查看行业ID是已经在导出库中存在，没有则添加，否则跳过
	public Industry isIndustryExist(String industryId) {
		cn.com.generaldata.os.pojo.Industry ui = industryDAO.get(cn.com.generaldata.os.pojo.Industry.class, industryId);
		if (ui != null) {
			return ui;
		}
		return null;
	}

	// 首先查看用户ID是已经在导出库中存在，没有则添加，否则跳过
	public UserIndustry isUserIndustryExist(String uiId) {
		cn.com.generaldata.os.pojo.UserIndustry ui = userIndustryDAO.get(cn.com.generaldata.os.pojo.UserIndustry.class,
				uiId);
		if (ui != null) {
			return ui;
		}
		return null;
	}

	// 首先查看用户ID是已经在导出库中存在，没有则添加，否则跳过
	public EnterpriseIndustry isEnterpriseIndustryExist(String eiId) {
		cn.com.generaldata.os.pojo.EnterpriseIndustry ei = enterpriseIndustryDAO.get(
				cn.com.generaldata.os.pojo.EnterpriseIndustry.class, eiId);
		if (ei != null) {
			return ei;
		}
		return null;
	}

	// 将业务库的已更新用户导入到导出库user_flag=0

	public boolean importUser() {

		List<cn.com.generaldata.os.importpojo.User> list = userDAOImport.find(
				cn.com.generaldata.os.importpojo.User.class, null, null);
		for (cn.com.generaldata.os.importpojo.User user : list) {
			String unitId = user.getUserUnit1();
			String areaId = user.getArea1();
			//			Filter areaFilter = new Filter();
			//			areaFilter.addRule(new FilterRule(FilterConstant.EQUAL, "id", areaId));
			cn.com.generaldata.os.importpojo.Area areal = areaDAOImport.get(
					cn.com.generaldata.os.importpojo.Area.class, areaId);

			// 导出区域
			if (areal != null) {
				//				cn.com.generaldata.os.importpojo.Area area = areaList.get(0);
				Area areaE = isAreaIdExist(areaId);
				if (areaE == null)
					areaE = new Area();
				BeanUtils.copyProperties(areal, areaE);
				areaDAO.saveOrUpdate(areaE);
			}
			//			Filter unitFilter = new Filter();
			//			unitFilter.addRule(new FilterRule(FilterConstant.EQUAL, "id", unitId));
			cn.com.generaldata.os.importpojo.Unit unitl = unitDAOImport.get(
					cn.com.generaldata.os.importpojo.Unit.class, unitId);
			// 导出单位
			if (unitl != null) {
				//				cn.com.generaldata.os.importpojo.Unit unit = unitList.get(0);
				Unit unitE = isUnitIdExist(unitId);
				if (unitE == null)
					unitE = new Unit();
				BeanUtils.copyProperties(unitl, unitE);
				if (unitl.getArea1() != null) {
					Area area = new Area();
					area.setId(unitl.getArea1());
					unitE.setArea(area);
				}
				if (unitl.getLarea1() != null) {
					//					Filter lareaFilter = new Filter();
					//					lareaFilter.addRule(new FilterRule(FilterConstant.EQUAL, "id", unitl.getLarea1()));
					cn.com.generaldata.os.importpojo.LArea lareal = lareaDAOImport.get(
							cn.com.generaldata.os.importpojo.LArea.class, unitl.getLarea1());

					//						// 导出村级区域
					if (lareal != null) {
						//						cn.com.generaldata.os.importpojo.LArea area = lareaList.get(0);
						LArea areaE = isLAreaIdExist(unitl.getLarea1());
						if (areaE == null)
							areaE = new LArea();
						BeanUtils.copyProperties(lareal, areaE);
						lareaDAO.saveOrUpdate(areaE);
					}

					LArea area = new LArea();
					area.setId(unitl.getLarea1());
					unitE.setLarea(area);
				}
				if (unitl.getRegistArea1() != null) {
					Area area = new Area();
					area.setId(unitl.getRegistArea1());
					unitE.setRegistArea(area);
				}
				if (unitl.getRegistLArea1() != null) {
					LArea area = new LArea();
					area.setId(unitl.getLarea1());
					unitE.setRegistLArea(area);
				}
				if (unitl.getRole1() != null) {
					Role area = new Role();
					area.setId(unitl.getRole1());
					unitE.setRole(area);
				}
				unitDAO.saveOrUpdate(unitE);
			}

			// 导出用户对应的企业行业分组和报表制度行业分组
			Filter userIndustryFilter = new Filter();
			userIndustryFilter.addRule(new FilterRule(FilterConstant.EQUAL, "user1", user.getId()));
			List<cn.com.generaldata.os.importpojo.UserIndustry> uilist = userIndustryDAOImport.find(
					cn.com.generaldata.os.importpojo.UserIndustry.class, userIndustryFilter, null);
			if (uilist != null && uilist.size() > 0) {
				for (cn.com.generaldata.os.importpojo.UserIndustry ui : uilist) {
					String industryId = ui.getIndustry1();
					Filter industryFilter = new Filter();
					industryFilter.addRule(new FilterRule(FilterConstant.EQUAL, "id", industryId));
					// 导出行业
					cn.com.generaldata.os.importpojo.Industry industryList = industryDAOImport.get(
							cn.com.generaldata.os.importpojo.Industry.class, industryId);
					if (industryList != null) {
						//						cn.com.generaldata.os.importpojo.Industry industry = industryList.get(0);
						cn.com.generaldata.os.pojo.Industry inE = isIndustryExist(industryId);
						if (inE == null)
							inE = new Industry();
						BeanUtils.copyProperties(industryList, inE);
						industryDAO.saveOrUpdate(inE);
					}
					UserIndustry uiE = isUserIndustryExist(ui.getId());
					if (uiE == null)
						// 导出报表制度行业分组
						uiE = new cn.com.generaldata.os.pojo.UserIndustry();
					BeanUtils.copyProperties(ui, uiE);
					if (ui.getIndustry1() != null) {
						cn.com.generaldata.os.pojo.Industry inE = new cn.com.generaldata.os.pojo.Industry();
						inE.setId(ui.getIndustry1());
						uiE.setIndustry(inE);
					}

					if (ui.getUser1() != null) {
						cn.com.generaldata.os.pojo.User inE = new cn.com.generaldata.os.pojo.User();
						inE.setId(ui.getUser1());
						uiE.setUser(inE);
					}
					userIndustryDAO.saveOrUpdate(uiE);
				}

			}
			List<cn.com.generaldata.os.importpojo.EnterpriseIndustry> eilist = enterpriseIndustryDAOImport.find(
					cn.com.generaldata.os.importpojo.EnterpriseIndustry.class, userIndustryFilter, null);
			if (eilist != null && eilist.size() > 0) {
				for (cn.com.generaldata.os.importpojo.EnterpriseIndustry ei : eilist) {
					String industryId = ei.getIndustry1();
					Filter industryFilter = new Filter();
					industryFilter.addRule(new FilterRule(FilterConstant.EQUAL, "id", industryId));
					// 导出行业
					cn.com.generaldata.os.importpojo.Industry industryList = industryDAOImport.get(
							cn.com.generaldata.os.importpojo.Industry.class, industryId);
					if (industryList != null) {
						//						cn.com.generaldata.os.importpojo.Industry industry = industryList.get(0);
						cn.com.generaldata.os.pojo.Industry inE = isIndustryExist(industryId);
						if (inE == null)
							inE = new Industry();
						BeanUtils.copyProperties(industryList, inE);
						industryDAO.saveOrUpdate(inE);
					}
					EnterpriseIndustry eiE = isEnterpriseIndustryExist(ei.getId());
					if (eiE == null)
						// 导出企业行业分组
						eiE = new cn.com.generaldata.os.pojo.EnterpriseIndustry();
					BeanUtils.copyProperties(ei, eiE);
					if (ei.getIndustry1() != null) {
						cn.com.generaldata.os.pojo.Industry inE = new cn.com.generaldata.os.pojo.Industry();
						inE.setId(ei.getIndustry1());
						eiE.setIndustry(inE);
					}
					if (ei.getUser1() != null) {
						cn.com.generaldata.os.pojo.User inE = new cn.com.generaldata.os.pojo.User();
						inE.setId(ei.getUser1());
						eiE.setUser(inE);
					}
					enterpriseIndustryDAO.saveOrUpdate(eiE);
				}
			}
			User userE = isUserIdExist(user.getId());
			String userPasswd = "";
			if (userE == null)
				// 导出用户
				userE = new cn.com.generaldata.os.pojo.User();
			else
				userPasswd = userE.getUserPasswd();
			BeanUtils.copyProperties(user, userE);
			if (user.getArea1() != null) {
				Area area = new Area();
				area.setId(user.getArea1());
				userE.setArea(area);
			}
			if (user.getRole1() != null) {
				Role area = new Role();
				area.setId(user.getRole1());
				userE.setRole(area);
			}
			if (user.getUserUnit1() != null) {
				Unit area = new Unit();
				area.setId(user.getUserUnit1());
				userE.setUserUnit(area);
			}
			if (!"".equals(userPasswd))
				userE.setUserPasswd(userPasswd);
			userE.setUserFlag("1");
			userDAO.saveOrUpdate(userE);
		}

		System.out.print("user clear!");
		return true;
	}

	// 首先查看用户ID是已经在导出库中存在，没有则添加，否则跳过
	public User isUserIdExist(String userId) {
		cn.com.generaldata.os.pojo.User user = userDAO.get(cn.com.generaldata.os.pojo.User.class, userId);
		if (user != null) {
			return user;
		}
		return null;
	}

	// 更改用户导出完成状态

	public boolean exportOverUser(String[] ids) {
		for (String uid : ids) {
			User user = userDAO.get(User.class, uid);
			user.setUserFlag("1");
			userDAO.update(user);
		}
		return true;
	}

	// 首先查看报表ID是已经在导出库中存在，没有则添加，否则跳过
	public boolean isReportIdExist(String reportId) {
		cn.com.generaldata.os.exportpojo.Report report = reportDAOExport.get(
				cn.com.generaldata.os.exportpojo.Report.class, reportId);
		if (report != null) {
			return true;
		}
		return false;
	}

	// 首先查看报表详细ID是已经在导出库中存在，没有则添加，否则跳过
	public boolean isReportDetailIdExist(String rdId) {
		cn.com.generaldata.os.exportpojo.ReportDetail report = reportDetailDAOExport.get(
				cn.com.generaldata.os.exportpojo.ReportDetail.class, rdId);
		if (report != null) {
			return true;
		}
		return false;
	}

	// 首先查看报表历史ID是已经在导出库中存在，没有则添加，否则跳过
	public boolean isReportHistoryIdExist(String rdId) {
		cn.com.generaldata.os.exportpojo.ReportHistory report = reportHistoryDAOExport.get(
				cn.com.generaldata.os.exportpojo.ReportHistory.class, rdId);
		if (report != null) {
			return true;
		}
		return false;
	}

	// 首先查看报表标记ID是已经在导出库中存在，没有则添加，否则跳过
	public boolean isReportMarkIdExist(String rdId) {
		cn.com.generaldata.os.exportpojo.ReportMarking report = reportMarkingDAOExport.get(
				cn.com.generaldata.os.exportpojo.ReportMarking.class, rdId);
		if (report != null) {
			return true;
		}
		return false;
	}

	// 首先查看报表ID是已经在存在，没有则添加，否则跳过
	public Report existReport(String reportId) {
		Report report = reportDAO.get(Report.class, reportId);
		if (report != null) {
			return report;
		}
		return null;
	}

	// 首先查看报表详细ID是已经存在，没有则添加，否则跳过
	public ReportDetail existReportDetail(String rdId) {
		ReportDetail report = reportDetailDAO.get(ReportDetail.class, rdId);
		if (report != null) {
			return report;
		}
		return null;
	}

	// 首先查看报表历史ID是已经存在，没有则添加，否则跳过
	public ReportHistory existReportHistory(String rdId) {
		ReportHistory report = reportHistoryDAO.get(ReportHistory.class, rdId);
		if (report != null) {
			return report;
		}
		return null;
	}

	// 首先查看报表标记ID是已经存在，没有则添加，否则跳过
	public ReportMarking existReportMarking(String rdId) {
		ReportMarking report = reportMarkingDAO.get(ReportMarking.class, rdId);
		if (report != null) {
			return report;
		}
		return null;
	}

	class ImportTask implements Runnable {

		@Override
		public void run() {
			importRunning = true;
			try {
				List<cn.com.generaldata.os.vo.ReportVO> reports = new ArrayList<cn.com.generaldata.os.vo.ReportVO>();
				// 需要导入报表
				List<cn.com.generaldata.os.importpojo.Report> imReports = reportDAOImport.find(
						cn.com.generaldata.os.importpojo.Report.class, null, null);
				if (imReports != null && imReports.size() > 0) {
					for (cn.com.generaldata.os.importpojo.Report im : imReports) {
						Report report = reportDAO.get(Report.class, im.getId());
						if (report != null) {
							BeanUtils.copyProperties(im, report);
							Area area = new Area();
							area.setId(im.getArea1());
							report.setArea(area);
							User operator = new User();
							operator.setId(im.getOperator1());
							report.setOperator(operator);
							User user = new User();
							user.setId(im.getUser1());
							report.setUser(user);
							report.setReportFlag("1");
							reportDAO.update(report);
						} else {
							report = new Report();
							BeanUtils.copyProperties(im, report);
							Area area = new Area();
							area.setId(im.getArea1());
							report.setArea(area);
							User operator = new User();
							operator.setId(im.getOperator1());
							report.setOperator(operator);
							User user = new User();
							user.setId(im.getUser1());
							report.setUser(user);
							report.setReportFlag("1");
							reportDAO.save(report);
						}
						Filter unitFilter = new Filter();
						unitFilter.addRule(new FilterRule(FilterConstant.EQUAL, "reportId", im.getId()));
						List<ReportUnit> ruList = reportUnitDAO.find(ReportUnit.class, unitFilter, null);
						List<cn.com.generaldata.os.importpojo.ReportUnit> find = reportUnitImportDAO.find(
								cn.com.generaldata.os.importpojo.ReportUnit.class, unitFilter, null);
						if (find != null && find.size() > 0) {
							reportUnitDAO.delete(ruList);
							for (cn.com.generaldata.os.importpojo.ReportUnit ru : find) {
								// 导出报表审批记录
								ReportUnit ruE = new ReportUnit();
								BeanUtils.copyProperties(ru, ruE);
								reportUnitDAO.save(ruE);
							}
						}
						Filter detailFilter = new Filter();
						detailFilter.addRule(new FilterRule(FilterConstant.EQUAL, "report1", im.getId()));
						List<cn.com.generaldata.os.importpojo.ReportDetail> imReportDetails = reportDetailDAOImport
								.find(cn.com.generaldata.os.importpojo.ReportDetail.class, detailFilter, null);
						Filter detailWebFilter = new Filter();
						detailWebFilter.addRule(new FilterRule(FilterConstant.EQUAL, "report.id", im.getId()));
						List<ReportDetail> find2 = reportDetailDAO.find(ReportDetail.class, detailWebFilter, null);
						if (imReportDetails != null && imReportDetails.size() > 0) {
							reportDetailDAO.delete(find2);
							for (cn.com.generaldata.os.importpojo.ReportDetail imDetail : imReportDetails) {
								ReportDetail reportDetail = new ReportDetail();
								BeanUtils.copyProperties(imDetail, reportDetail);
								Report report2 = new Report();
								report2.setId(imDetail.getReport1());
								reportDetail.setReport(report2);
								reportDetailDAO.save(reportDetail);
							}
						}
						List<cn.com.generaldata.os.importpojo.ReportHistory> imReportHistorys = reportHistoryDAOImport
								.find(cn.com.generaldata.os.importpojo.ReportHistory.class, detailFilter, null);
						List<ReportHistory> find3 = reportHistoryDAO.find(ReportHistory.class, detailWebFilter, null);

						if (imReportHistorys != null && imReportHistorys.size() > 0) {
							reportHistoryDAO.delete(find3);
							for (cn.com.generaldata.os.importpojo.ReportHistory imHistory : imReportHistorys) {
								ReportHistory reportHistory = new ReportHistory();
								BeanUtils.copyProperties(imHistory, reportHistory);
								Report report2 = new Report();
								report2.setId(imHistory.getReport1());
								reportHistory.setReport(report2);
								User operator = new User();
								operator.setId(imHistory.getOperator1());
								reportHistory.setOperator(operator);
								reportHistoryDAO.save(reportHistory);
							}
						}

						List<cn.com.generaldata.os.importpojo.ReportMarking> imReportMarkings = reportMarkingDAOImport
								.find(cn.com.generaldata.os.importpojo.ReportMarking.class, detailFilter, null);

						List<ReportMarking> find4 = reportMarkingDAO.find(ReportMarking.class, detailWebFilter, null);

						if (imReportMarkings != null && imReportMarkings.size() > 0) {
							reportMarkingDAO.delete(find4);
							for (cn.com.generaldata.os.importpojo.ReportMarking imMarking : imReportMarkings) {
								ReportMarking reportMarking = new ReportMarking();
								BeanUtils.copyProperties(imMarking, reportMarking);
								Report report2 = new Report();
								report2.setId(imMarking.getReport1());
								reportMarking.setReport(report2);
								User operator = new User();
								operator.setId(imMarking.getOperator1());
								reportMarking.setOperator(operator);
								reportMarkingDAO.save(reportMarking);
							}

						}
						cn.com.generaldata.os.vo.ReportVO reportVO = new cn.com.generaldata.os.vo.ReportVO();
						BeanUtils.copyProperties(report, reportVO);
						if (report.getArea() != null) {
							reportVO.setAreaId(report.getArea().getId());
						}
						if (report.getOperator() != null) {
							reportVO.setOperatorId(report.getOperator().getId());
							reportVO.setOperatorName(report.getOperator().getUserLoginname());
						}
						if (report.getUser() != null) {
							reportVO.setUserId(report.getUser().getId());
						}
						reports.add(reportVO);
					}
				}

				if (reports.size() > 0) { // 存在导入数据，导入后清空导入库数据
					cleanImReport(); // 清空导入库数据
				}
			} catch (BeansException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				importRunning = false;
			}
			importRunning = false;

			System.out.print("report clear!");
		}
	}

	// 将导出入库导入到业务库的已上报报表

	public List<cn.com.generaldata.os.vo.ReportVO> importEIReport() {
		if (!importRunning) {
			executorService.execute(new Runnable() {

				@Override
				public void run() {
					importRunning = true;
					importUser();
					new ExportTypeTask().run();
					new ImportTask().run();
					importRunning = false;
				}
			});
			//importService.execute(new ImportTask());
		}
		return null;
	}

	// 自动清除导出库的数据

	public Boolean cleanEIReport() {
		List<cn.com.generaldata.os.exportpojo.ReportMarking> rmlist = reportMarkingDAOExport.find(
				cn.com.generaldata.os.exportpojo.ReportMarking.class, null, null);
		reportMarkingDAOExport.delete(rmlist);
		List<cn.com.generaldata.os.exportpojo.ReportHistory> rhlist = reportHistoryDAOExport.find(
				cn.com.generaldata.os.exportpojo.ReportHistory.class, null, null);
		reportHistoryDAOExport.delete(rhlist);
		List<cn.com.generaldata.os.exportpojo.ReportDetail> rdlist = reportDetailDAOExport.find(
				cn.com.generaldata.os.exportpojo.ReportDetail.class, null, null);
		reportDetailDAOExport.delete(rdlist);
		List<cn.com.generaldata.os.exportpojo.Report> rlist = reportDAOExport.find(
				cn.com.generaldata.os.exportpojo.Report.class, null, null);
		reportDAOExport.delete(rlist);

		return true;

	}

	// 自动清除导入库的数据
	public void cleanImReport() {
		List<cn.com.generaldata.os.importpojo.ReportMarking> rmlist = reportMarkingDAOImport.find(
				cn.com.generaldata.os.importpojo.ReportMarking.class, null, null);
		reportMarkingDAOImport.delete(rmlist);

		List<cn.com.generaldata.os.importpojo.ReportHistory> rhlist = reportHistoryDAOImport.find(
				cn.com.generaldata.os.importpojo.ReportHistory.class, null, null);
		reportHistoryDAOImport.delete(rhlist);
		List<cn.com.generaldata.os.importpojo.ReportDetail> rdlist = reportDetailDAOImport.find(
				cn.com.generaldata.os.importpojo.ReportDetail.class, null, null);
		reportDetailDAOImport.delete(rdlist);
		List<cn.com.generaldata.os.importpojo.Report> rlist = reportDAOImport.find(
				cn.com.generaldata.os.importpojo.Report.class, null, null);
		reportDAOImport.delete(rlist);
		List<cn.com.generaldata.os.importpojo.UserIndustry> uilist = userIndustryDAOImport.find(
				cn.com.generaldata.os.importpojo.UserIndustry.class, null, null);
		userIndustryDAOImport.delete(uilist);
		List<cn.com.generaldata.os.importpojo.User> ulist = userDAOImport.find(
				cn.com.generaldata.os.importpojo.User.class, null, null);
		userDAOImport.delete(ulist);
	}

	// 清除数据

	public void deleteReports(List<String> ids) {
		String[] arrIds = new String[ids.size()];
		ids.toArray(arrIds);

		Filter filter = new Filter();
		filter.addRule(new FilterRule(FilterConstant.COLLECTION, "id", arrIds));
		List<Report> reports = reportDAO.find(Report.class, filter, null);
		for (Report report : reports) {
			Filter filter2 = new Filter();
			filter2.addRule(new FilterRule(FilterConstant.EQUAL, "report.id", report.getId()));
			List<ReportMarking> list = reportMarkingDAO.find(ReportMarking.class, filter2, null);
			reportMarkingDAO.delete(list);
			List<ReportHistory> find = reportHistoryDAO.find(ReportHistory.class, filter2, null);
			reportHistoryDAO.delete(find);
			List<ReportDetail> find2 = reportDetailDAO.find(ReportDetail.class, filter2, null);
			reportDetailDAO.delete(find2);
			Filter filter3 = new Filter();
			filter3.addRule(new FilterRule(FilterConstant.EQUAL, "reportId", report.getId()));
			List<ReportUnit> find3 = reportUnitDAO.find(ReportUnit.class, filter3, null);
			reportUnitDAO.delete(find3);
			reportDAO.delete(report);
		}
	}

	/* 判断字符串是否为空,当判断的为从数据库中提取的字段时，得加上判断null */
	public static Boolean NotisNULL(String para) {
		if (para != null && !para.trim().equals("") && !para.trim().equals("null")) {
			return true;
		} else {
			return false;
		}
	}

	// 首先查看用户ID是已经在导出库中存在，没有则添加，否则跳过
	public boolean isUserIdExport(String userId) {
		cn.com.generaldata.os.exportpojo.User user = userDAOExport.get(cn.com.generaldata.os.exportpojo.User.class,
				userId);
		if (user != null) {
			return true;
		}
		return false;
	}

	// 查看单位ID是已经在导出库中存在，没有则添加，否则跳过
	public boolean isUnitIdExport(String unitId) {
		UnitNz unit = daoExport.fetch(UnitNz.class, unitId);
		if (unit != null) {
			return true;
		}
		return false;
	}

	public boolean exportUser(String[] ids) {
		for (String uid : ids) {
			if (!isUserIdExport(uid)) {
				User user = userDAO.get(User.class, uid);
				Unit userUnit = user.getUserUnit();

				// 导出单位
				if (userUnit != null && !isUnitIdExport(userUnit.getId())) {
					cn.com.generaldata.os.exportpojo.Unit unitE = new cn.com.generaldata.os.exportpojo.Unit();
					BeanUtils.copyProperties(userUnit, unitE);
					if (userUnit.getArea() != null) {
						unitE.setArea1(userUnit.getArea().getId());
					}
					if (userUnit.getLarea() != null) {
						unitE.setLarea1(userUnit.getLarea().getId());
					}
					if (userUnit.getRegistArea() != null) {
						unitE.setRegistArea1(userUnit.getRegistArea().getId());
					}
					if (userUnit.getRegistLArea() != null) {
						unitE.setRegistLArea1(userUnit.getRegistLArea().getId());
					}
					if (userUnit.getRole() != null) {
						unitE.setRole1(userUnit.getRole().getId());
					}
					unitDAOExport.saveOrUpdate(unitE);
				}
				// 导出用户
				cn.com.generaldata.os.exportpojo.User userE = new cn.com.generaldata.os.exportpojo.User();
				BeanUtils.copyProperties(user, userE);
				if (user.getArea() != null) {
					userE.setArea1(user.getArea().getId());
				}
				if (user.getRole() != null) {
					userE.setRole1(user.getRole().getId());
				}
				if (user.getUserUnit() != null) {
					userE.setUserUnit1(user.getUserUnit().getId());
				}
				userDAOExport.saveOrUpdate(userE);
			}
		}
		exportOverUser(ids);
		System.out.print("clear!");
		return true;
	}
}
